/* 
 Project     : 48c6c450f1a4a0cc53d9585dc0fee742
 Created on  : Mar 16, 2013, 11:29:15 PM
 Author      : Truong Khuong - khuongxuantruong@gmail.com
 Description :
 Purpose of the stylesheet follows.
 */
var GALLERY_CONFIG = {
    JQXGRID_ELEMENT: '#data-table',
    CATE_PARENT: '',
    BINDING_URI: base_url + 'backend/wallpaper/binding/',
    EDIT_PANEL_URI: base_url + "backend/wallpaper/EditPanel",
    EDIT_PANEL_URI2: base_url + "backend/wallpaper/EditPanelIn",
    COMMIT_URI: base_url + "backend/wallpaper/Commit",
    DELETE_URI: base_url + "backend/excution/delete/gallery/album/albu",
    PENDING: false
};
var _GALLERY = (function() {
    var init = false;
    return {
        OnInit: function() {
            try {
                if (init)
                    return;
                init = true;

            } catch (e) {
                bootbox.alert(e.message);
            }
        },
        AddItemIn: function() {
            this.EditItemIn(0);
        },
        EditItemIn: function(Id) {

            if ($('#_form_edit_panel').length == 1)
                return;
            if (PENDING)
                return false;
            new backend({
                url: GALLERY_CONFIG.EDIT_PANEL_URI2,
                data: {
                    Id: Id,
                    CateId: GALLERY_CONFIG.CATE_PARENT
                },
                callback: function(rsdata) {
                    PENDING = false;
                    if (rsdata.result < 0) {
                        $('#_edit_panel_insite').html(rsdata.message).show();
                    } else {
						$("#_form_edit_panel").validationEngine({validateNonVisibleFields:true,scroll:false});
                        $('#viewport_gallery').hide();
                        $('#_edit_panel_insite').show().html(rsdata.message);
						//$("#albu_fcate").uniform({radioClass: 'choice'});
                        //$(".collapse").collapse();
//                        $('#_edit_panel_tabs').jqxTabs({
//                            theme: 'metro', height: 'auto',  scrollable: true, 
//                            selectionTracker: true, animationType: 'fade'
//                        });
                        //$('#albu_fcate').selectpicker({cls: 'btn-sm'});
                        $('#_form_edit_panel select').select2({minimumResultsForSearch: -1});
						//$( "#_form_edit_panel input[type='number']" ).spinner();
                    }
                }
            }).call();
        },
		inValid:function(){
			if( $('#_form_edit_panel').validationEngine('validate') === false) return false;
			if($('#_form_edit_panel .album_images img.album-item').length==0){
				$.jGrowl('No photo selected',{theme: 'growl-error'});
				return false;
			}
			return true;
		},
        SaveIn: function() {
			if(!this.inValid())return;
            //if(GALLERY_CONFIG.PENDING) return false;
            var Params = $('#_form_edit_panel').serializeObject();
            //Params.cate_type=GALLERY_CONFIG.CATE_PARENT;

            var imgs = new Array();
            imgs = $('#_form_edit_panel .album_images img.album-item').map(function() {
                return this.src;
            });
            Params.albu_images = new Array();
            for (var i = 0; i < imgs.length; i++) {
                Params.albu_images[i] = imgs[i];

            }
            GALLERY_CONFIG.PENDING = true;
            new backend({
                url: GALLERY_CONFIG.COMMIT_URI,
                data: {
                    Params: Params,
                    Id: $('#albu_id').val()
                },
                callback: function(rsdata) {
                    GALLERY_CONFIG.PENDING = false;
                    if (rsdata.result < 0) {
                        $.jGrowl(rsdata.message, {sticky: true, theme: 'growl-error', header: 'Error!'});
                        return false;
                    } else {
                        $.jGrowl(rsdata.message);
                        _GALLERY.CancelIn();
                        _GALLERY.Refresh();
                        return true;
                    }
                }
            }).call();
            return false;
        },
        OtherAdvand: function() {
            $('.otheradvand').toggle();
        },
        CancelIn: function() {
            $('#viewport_gallery').show();
            $('#_edit_panel_insite').hide().html('');
        },
        DeleteItem: function(id) {
            backend_delete(GALLERY_CONFIG.DELETE_URI,id,function(){
                oTable.fnDraw(false);
            });

        },
        RestoreItem: function() {
            accessdenied();
        },
        Permission: function(id) {

        },
        PrivateItem: function() {
            accessdenied();
        },
        Setting: function() {

        },
        Refresh: function() {
            //(GALLERY_CONFIG.JQXGRID_ELEMENT).dataTable().fnDraw();
            oTable.fnDraw(false);
        },
        Filter: function(filtervalue) {
            $(GALLERY_CONFIG.JQXGRID_ELEMENT).dataTable().fnFilter(filtervalue, 2);
        },
        ChooseImages: function() {
            new backend({
                url: base_url+"backend/filemanage/switchFileType/",
                data: {
                    Type:$('#albu_subfolder').val()
                },
                callback: function(rsdata) {
                    if (rsdata.result < 0) {
                        $.jGrowl(rsdata.message, {sticky: true, theme: 'growl-error', header: 'Error!'});
                    } else {
                        var ckpath='libraries/kcfinder/browse.php?lang=en&type=kitchens&dir=kitchens/'+$('#albu_subfolder').val();
                        //var ckpath='libraries/kcfinder/browse.php?lang=en&type='+$('#albu_subfolder').val();
                        openKCFinderMultiByPath(
                                ckpath
                            ,function(files) {
                            var str = '';
                            for (var i = 0; i < files.length; i++) {
                                if ($('.thumbnail').length + i >= 5)
                                    break;
                                str += '<div class="col-sm-3 col-xs-4 col-md-3 span2 photo-thumb-item">\
                                            <div class="thumbnail widget" style="position: relative">\
                                                <div class="well">\
                                                                                            <div class="view">\
                                                                                                    <span onclick="_GALLERY.DeletePhoto(this)" style="position: absolute;top: -10px;right: -10px" class="ico-remove" title="Delete the Photo"></span>\
                                                <img class="album-item" data-src="holder.js/300x200" src="' + files[i] + '" alt="...">\\n\
                                                <span class="moveup icon-arrow-left" title="Move up" onclick="moveup(this)"></span>\
                                                <span class="movedown icon-arrow-right" title="Move down" onclick="movedown(this)"></span>\
                                            </div></div></div>\
                                        </div>' + "\n";
                            }
                            $('.album_images').append(str);
                        });
                    }
                }
            }).call();
            

        },
		DeletePhoto:function(me){
			$(me).parents('.photo-thumb-item').remove();
		}
    };
}());
$(document).ready(function() {
    //Btree().Init();
});
function moveup(me){
    var img=$(me).parents('.photo-thumb-item').find('img.album-item');
    var $p= $('.album_images .photo-thumb-item img.album-item').index(img);
    if($p>0){
        var imgu=$('.album_images .photo-thumb-item img.album-item').eq($p-1);
        var tmp=img.attr('src');
        img.attr('src',imgu.attr('src'));
        imgu.attr('src',tmp);
        console.log($('.album_images .photo-thumb-item img.album-item').map(function() {
                return this.src;
            }));
    }
}
function movedown(me){
    var img=$(me).parents('.photo-thumb-item').find('img.album-item');
    var $p= $('.album_images .photo-thumb-item img.album-item').index(img);
    if($p<$('.album_images .photo-thumb-item img.album-item').length-1){
        var imgu=$('.album_images .photo-thumb-item img.album-item').eq($p+1);
        var tmp=img.attr('src');
        img.attr('src',imgu.attr('src'));
        imgu.attr('src',tmp);
        console.log($('.album_images .photo-thumb-item img.album-item').map(function() {
                return this.src;
            }));
    }
}